#include <iostream>
#include <time.h>
using namespace std;

class CSplit
{
public:
    CSplit();
    ~CSplit();
    char* operator[] (int iIndex)
    {
        return m_pSplit[iIndex];
    }
    int GetCount() { return m_iCount; }
    void Split(char* sString, char cSpliter)
    {
        m_iCount = 0;
        m_pSplit[m_iCount++] = sString;
        for (char* p = sString; *p != '\0'; ++p)
        {
            if (*p == cSpliter)
            {
                m_pSplit[m_iCount++] = p + 1;
                *p = '\0';
            }
        }
    }
private:
    char* m_pSplit[1024];
    int m_iCount = 0;
};

CSplit::CSplit()
{
}

CSplit::~CSplit()
{
}

class CFieldHelper
{
public:
    void Handle(char *sString, int iLen)
    {
        int iIndex = 0;
        int iDataLen = 0;
        m_iCount = 0;
        for (int iIndex = 0; iIndex+4 < iLen;)
        {
            iDataLen = (sString[iIndex] - '0') * 1000;
            iDataLen += (sString[iIndex+1] - '0') * 100;
            iDataLen += (sString[iIndex + 2] - '0') * 10;
            iDataLen += (sString[iIndex + 3] - '0');
            iIndex += 4;
            m_pSplit[m_iCount++] = sString + iIndex;
            iIndex += iDataLen;
            sString[iIndex] = '\0';
            iIndex += 1;
        }
    }

    char* operator[] (int iIndex)
    {
        return m_pSplit[iIndex];
    }
    int GetCount() { return m_iCount; }

private:
    char* m_pSplit[1024];
    int m_iCount = 0;
};

int main(int argc, char* argv[])
{
    char sBuff1[] = "11111,22222,33333,44444,55555,66666,77777,88888,99999,00000";
    char sBuff2[] = "000511111,000522222,000533333,000544444,000555555,000566666,000577777,000588888,000599999,000500000";
    CSplit oSplit;
    int iBuff1Len = strlen(sBuff1);
    int iTestCount = 100000000;
    time_t ttBefore = 0;
    time_t ttAfter = 0;
    ttBefore = time(NULL);
    char TempBuff[100] = { '\0' };
    for (int i = 0; i < iTestCount; i++)
    {
        memcpy(TempBuff, sBuff1, sizeof(sBuff1));
        oSplit.Split(TempBuff, ',');
    }
    ttAfter = time(NULL);
    cout << "UsedTime:" << ttAfter - ttBefore << endl;
    
    for (int i = 0; i < oSplit.GetCount(); i++)
    {
        cout << "Field" << i << ":"<< oSplit[i] << endl;
    }

    cout << endl;
    cout << endl;
    CFieldHelper oFieldHelper;
    int iBuff2Len = strlen(sBuff2);
    ttBefore = time(NULL);
    for (int i = 0; i < iTestCount; i++)
    {
        memcpy(TempBuff, sBuff2, sizeof(sBuff2));
        oFieldHelper.Handle(TempBuff, iBuff2Len);
    }
    ttAfter = time(NULL);
    cout << "UsedTime:" << ttAfter - ttBefore << endl;

    for (int i = 0; i < oFieldHelper.GetCount(); i++)
    {
        cout << "Field" << i << ":" << oFieldHelper[i] << endl;
    }
    return 0;
}
